<?php

namespace App\Services;

use App\CodeResponse;
use App\Models\ClassStudent;
use App\Models\Course;
use App\Models\SchoolClassCourse;
use App\Models\SchoolCourse;

class CourseService extends BaseService
{
    /**
     * 用户所在学校班级是否有该课程
     * @return
     */
    public function getByUserCourse($users_id,$school_id,$class_grades_id,$course_id) {
        $query = SchoolCourse::query()->where('users_id',$school_id)->where('course_id',$course_id)->first();

    }

    /**
     * 获取列表
     * @return
     */
    public function list($page,$limit,$sort,$order,$name,$recommend,$course_categories_id,$course_lecturer_id,$free) {
        $query = Course::query();
        if(!empty($name)){
            $query->where('name','like',"%$name%");
        }
        if(isset($recommend)){
            $query->where('recommend',$recommend);
        }
        if(isset($course_categories_id)){
            $query->where('course_categories_id',$course_categories_id);
        }
        if(isset($course_lecturer_id)){
            $query->where('course_lecturer_id',$course_lecturer_id);
        }
        if(isset($free)){
            $query->where('free',$free);
        }
        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }

    /**
     * 获取课程信息
     * @return
     */
    public function getByCourseInfo($course_id) {
        $query = Course::leftJoin('course_categories', 'course_categories.id', '=', 'courses.course_categories_id')
                        ->leftJoin('course_lecturers', 'course_lecturers.id', '=', 'courses.course_lecturer_id')
                        ->select('courses.*','course_categories.name as categorie_name','course_lecturers.name as lecturer_name','course_lecturers.abstract as lecturer_abstract','course_lecturers.pic as lecturer_pic');
        return $query->where('courses.id',$course_id)->get();
    }
    /**
     * 获取不属于某个学校的全部课程
     * @return
     */
    public function getNotSchoolCourse($school_id) {
        $schoolCourse = SchoolCourse::where('school_id',$school_id)->select('school_courses.course_id')->get();
        $arr = [];
        foreach ($schoolCourse as  $v){
            array_push($arr,$v->course_id);
        }
        return Course::whereNotIn('id',$arr)->select('id','name')->get();

    }

    /**
     * 获取不属于某个学校班级的全部课程
     * @return
     */
    public function getNotSchoolClassCourse($school_id,$class_grades_id) {
        $schoolClassCourse = SchoolClassCourse::where('school_id',$school_id)
                        ->where('class_grades_id',$class_grades_id)
                        ->select('course_id')->get();
        $arr = [];
//        dd($schoolClassCourse);
        foreach ($schoolClassCourse as  $v){
            array_push($arr,$v->course_id);
        }
        return  SchoolCourse::leftJoin('courses', 'school_courses.course_id', '=', 'courses.id')
                ->where('school_id',$school_id)
                ->whereNotIn('courses.id',$arr)->select('courses.id','courses.name')->get();

    }
}
